var nFileCount = 0;
var bAdjustMode = 0;
var isStarted = 0;
var bCropArea = 0;
var Width = 0;
var Height = 0;
var quanlity=100;
var size = 0;
var captureWindow;


function StartPlay() {
	try{
	var b =CmCaptureOcx.Initial();
	b = CmCaptureOcx.StartRun(0);
	SetJpgQuanlity(quanlity);
	try{
	CmCaptureOcx.SetAdjustSkew(1);
	}catch(e){
		CmCaptureOcx.AutoCrop(true);
	}
	Ext.getCmp('kspz').disable();
	isStarted=1;
	}catch(e){
//		Destory();
	}
}

function Capture(RandomNo){	
	var strFile = "c:\\" + RandomNo;
	strFile += ".jpg";
	var b = CmCaptureOcx.CaptureImage(strFile);
	return {
		strFile:strFile,
		success : b
	};
}

function SaveColor(spOrgId) {
	var r = ExtJ.getRandomNo();
	var localData  = Capture(r);
	var durl = ExtJ.getActionPath('com.suyuan.global.action.UploadAction', 'downloadPic',{picSer : r});
	var uUrl = ExtJ.getActionPath('com.suyuan.global.action.UploadAction', 'uploadPic',{picSer : r,orgId : spOrgId});
	var b = CmCaptureOcx.UpdataFile(uUrl,localData.strFile);//,true
	return {
		name : r,
		url : durl,
		success : localData.success
	};
}

function Adjust()
{
	if(bAdjustMode == 0)
	{
		CmCaptureOcx.SetAdjustSkew(1);
		bAdjustMode = 1;
		ExtJ.showMessage("自动裁切");
	}
	else
	{
		CmCaptureOcx.SetAdjustSkew(0);
		bAdjustMode = 0;
		bCropArea = 1;
		CmCaptureOcx.SetCropArea(bCropArea);
		ExtJ.showMessage("手动裁切");
	}
}
function SetCropArea()
{
	if(bCropArea == 0)
	{	
		try{
		bCropArea = 1;
		CmCaptureOcx.SetCropArea(bCropArea);
		CmCaptureOcx.SetAdjustSkew(0);
//		ExtJ.showMessage("手动裁切设置成功");
	}catch(e){
			CmCaptureOcx.AutoCrop(false);
			CmCaptureOcx.CusCrop(true);
			
	}
	}
	else
	{
		try{
		bCropArea = 0;
		CmCaptureOcx.SetCropArea(bCropArea);
		CmCaptureOcx.SetAdjustSkew(1);
//		ExtJ.showMessage("自动裁切设置成功");
		}catch(e){
			
			CmCaptureOcx.CusCrop(false);
			CmCaptureOcx.AutoCrop(true);
		}
	}
}

function Destory(){
	CmCaptureOcx.Destory();
}

function SetJpgQuanlity(quanlity){
	CmCaptureOcx.SetJpgQuanlity(quanlity);
}
/**
 * Ext.ux.UploadPanel
 * 
 */

Ext.namespace("Ext.ux.panel");
Ext.ux.panel.PicUploadPanel = function(config) {
	Ext.apply(this, config);
	this.delFileId = '';
	this.initComponentUI();
	if(this.quanlity){
		quanlity=this.quanlity;
	}
	Ext.ux.panel.PicUploadPanel.superclass.constructor.call(this);
};

Ext.extend(Ext.ux.panel.PicUploadPanel, Ext.Panel, {
	readOnly : false,

	initComponentUI : function() {
		this.serNoIds = []
		this.comArr=[];
		this.picTools = [{
			id : 'plus',
			qtip : '拍摄图片',
			handler : this.onAddCaptureWindow.createDelegate(this)
		}];
		
		this.portal = new Ext.ux.Portal({
			region : 'center',
			// margins : '35 5 5 0',
			items : [{}]
		});

		if (this.readOnly) {
			this.picTools = [];
		}
		
		Ext.apply(this, {
			layout : 'border',
			items : [this.portal],
			tools : this.picTools,
			listeners : {
				resize : {
					fn : function() {
						this.portal.doLayout();
					}.createDelegate(this)
				}
			}
		});
	},
	onAddCaptureWindow : function() {
		if(!captureWindow){
		this.captureWindow = new Ext.Window({
			title : '拍摄窗口',
			layout : 'border',
			modal : true,
			// resizable : false,
			height : 460,
			width : 510,
			autoScroll : true,
			tbar : [{
				text : '开始拍照',
				tooltip : '',
				id : 'kspz',
				iconCls : 'edit',
				handler : function(){
					StartPlay();
				}
			}, {
				text : '手动裁切',
				id : 'SetCropArea',
				tooltip : '',
				iconCls : 'edit',
				handler : function(t){
				 	SetCropArea();
				 	if(bCropArea==1){
				 		t.setText('自动裁切');
				 	}else{
				 		t.setText('手动裁切');
				 	}
				}
			}
//			, {
//				text : '自动裁切',
//				tooltip : '',
//				iconCls : 'edit',
//				handler : function() {
//					Adjust();
//				}
//			}
			, {
				text : '拍照',
				tooltip : '',
				iconCls : 'edit',
				handler : function(){
					captureWindow.capturePanel.onCapture();
				}
			},{
				text : '关闭',
				tooltip : '',
				iconCls : 'edit',
				handler : function(){
					captureWindow.close();
				}
			}],
			items : [{
				region : 'center',
				html : '<OBJECT id="CmCaptureOcx" style=" WIDTH: 100%; HEIGHT:100%"	codeBase="CmCaptureOcx.ocx"	classid="clsid:3CA842C5-9B56-4329-A7CA-35CA77C7128D" ></OBJECT>'
			}]
		});
		captureWindow = this.captureWindow;
		}
		captureWindow.on('beforeclose', function() {
			captureWindow.hide();
			captureWindow.un('close');
		});
		captureWindow.capturePanel = this
		captureWindow.show();
		
	},
	onDelete : function() {
	},

	onSet : function() {

	},
	SetCropArea : function(){
		SetCropArea();
	},
	onRun : function() {
		StartPlay();
	},
	onCapture : function() {
		if(isStarted==0){
		alert('请点\"开始拍照\"');
		return ;
	}
		var tools = [{
			id : 'close',
			handler : function(e, target, panel) {
				var pId = panel.pId;
				Ext.MessageBox.confirm('删除', '您确认删除该图片吗?(' + pId + ')',
						function(btn) {
							if (btn == 'yes') {
								Ext.Ajax.request({
									url : ExtJ
											.getActionPath(
													'com.suyuan.global.action.UploadAction',
													'deletePic'),
									params : {
										pId : pId
									},
									success : function(response) {
										ExtJ.showMessage('删除成功！');
										panel.ownerCt.ownerCt.remove(
												panel.ownerCt, true);
												for(var p=0;this.serNoIds.length;p++){
													var s = this.serNoIds[p];
													if(pId==s){
														this.serNoIds[p]='';
														break;
													}
													
												}
									}.createDelegate(this),
									failure : function(response) {

									}
								});
							}
						}.createDelegate(this));
			}.createDelegate(this)
		}];
		var o = SaveColor(this.spOrgId);
		var url = o.url;
		var title = o.name;
		var success = o.success;
		if ('1' == success) {
			var p = new Ext.Panel({
					title : title,
					flag : false,
					autoScroll : true,
					url : url,
					pId : title,
					tools : tools,
					html : "<img id=" + title + " width=100% height=250 src='"
							+ url + "'/>"
				});
			this.portal.add({
				columnWidth : .5,
				style : 'padding:10px 0 0px 10px',
				items : [p]
			});
			this.portal.doLayout();
			this.comArr.push(p);
			this.serNoIds.push(title);
//			new Ext.Resizable(title, {
//				wrap : true,
//				pinned : true,
//				minWidth : 50,
//				minHeight : 50,
//				preserveRatio : true,
//				dynamic : true,
//				handles : 'all',
//				draggable : true
//			});
		}

	},
	onStop : function() {
		StopPlay();
	},
	onComplete : function(dialog, filename, resp) {
		this.grid.store.add(new Ext.data.Record({
			FILE_ID : resp.FILE_ID,
			FILE_NAME : resp.FILE_NAME,
			FILE_SIZE : resp.FILE_SIZE
		}));
	},

	getPicSers : function() {
		return this.serNoIds;
	},
	
	delPic : function(){
		
		for (var i = 0; i < this.comArr.length; i++) {
			var c = this.comArr[i];
			try{
			c.ownerCt.ownerCt.remove(c.ownerCt, true);
			}catch(e){}
		}
		this.portal.doLayout();
		this.comArr=[];
		this.serNoIds=[];
	},
	getDelFileIds : function() {
		return this.delFileId;
	},
	imgToSize : function (size,fileName) {
		var img = $('#'+fileName+'Pic');
		var oWidth=img.width(); //取得图片的实际宽度
		var oHeight=img.height(); //取得图片的实际高度
		if(oWidth <= 100 && size < 0){
			return;
		}
		img.width(oWidth + size);
		img.height(oHeight + size/oWidth*oHeight);
	},
	
	onDowloadAll : function(){
		if(!this.dowload_Ids){
			ExtJ.showMessage('没有图片可以下载！');
			return ;
		}
			Ext.MessageBox.confirm('下载', '你确定下载所有图片到一张吗？', function(btn) {
				if (btn == 'yes') {
					ExtJ.doExport({
						url : ExtJ
								.getActionPath(
										'com.suyuan.global.action.UploadAction',
										'downloadAllPic', {
											dowloadIds : this.dowload_Ids
										})
					});
				}
			}.createDelegate(this));
		
	},
	load : function(bizId,upload) {
		if(!upload)
		this.delPic();
		this.dowload_Ids=bizId;
		Ext.Ajax.request({
			url : ExtJ.getActionPath('com.suyuan.global.action.UploadAction',
					'listFiles'),
			params : {
				bizId : bizId
			},
			success : function(response) {
				var rep = Ext.decode(response.responseText);
				if (rep instanceof Array) {

					for (var index = 0; index < rep.length; index++) {
						var fileId = rep[index].FILE_ID;
						var fileName = rep[index].FILE_NAME;
						var downUrl = ExtJ.getActionPath(
								'com.suyuan.global.action.UploadAction',
								'downloadFile', {
									attachId : fileId
								});
						var loadTools = [{
							id : 'maximize',
							qtip :'放大',
							handler : function(e, target, panel) {
								var pId = panel.pId;
								var attach_Id = panel.attachId;
								var w = document.body.scrollWidth;
								var h = document.body.scrollHeight;
								var url = ExtJ.getActionPath(
								'com.suyuan.global.action.UploadAction',
								'downloadFile', {
									attachId : attach_Id
								});
							var w = new Ext.Window({
									title : pId,
									layout : 'border',
									modal : true,
									resizable : true,
									height : h-100,
									width : w-100,
									autoScroll : true,
									maximizable : true,
//									maximized : true,
									items : [{
									region : 'center',
									html : "<div style = 'position:absolute;overflow-x:auto;overflow-y:auto;width:100%;height:100%;'><img id ="
												+ fileName+'Pic'
												+ " align='center' width=100% height=100% src='"
												+ url + "'/></div>"
									}],
									tbar : [{
										id : 'picMaxSize',
										iconCls : 'max',
										text : '放大',
										handler : this.imgToSize.createDelegate(this,[50,fileName])
									},{
										id : 'picMinSize',
										iconCls : 'min',
										text : '缩小',
										handler : this.imgToSize.createDelegate(this,[-50,fileName])
									},{
										id : 'picRotate',
										iconCls : 'rotate',
										text : '旋转',
										handler : function(){
											$('#'+fileName+'Pic').rotateRight(90)
										}
									}]
								}).show();
								
							}.createDelegate(this)
						},{
							id : 'down',
							qtip :'下载',
							handler : function(e, target, panel) {
								var pId = panel.pId;
								Ext.MessageBox.confirm('下载', '你确定下载该图片吗？('
										+ pId + ')', function(btn) {
									if (btn == 'yes') {
										ExtJ.doExport({
											url : ExtJ
													.getActionPath(
															'com.suyuan.global.action.UploadAction',
															'downloadPic', {
																picSer : pId
															})
										});
									}
								}.createDelegate(this));
							}.createDelegate(this)
						}, {
							id : 'close',
							qtip :'删除',
							hidden : this.readOnly,
							handler : function(e, target, panel) {
								var pId = panel.pId;
								Ext.MessageBox.confirm('删除', '您确认删除该图片吗?('
										+ pId + ')', function(btn) {
									if (btn == 'yes') {
										Ext.Ajax.request({
											url : ExtJ
													.getActionPath(
															'com.suyuan.global.action.UploadAction',
															'deletePic'),
											params : {
												pId : pId
											},
											success : function(response) {
												ExtJ.showMessage('删除成功！');
												panel.ownerCt.ownerCt.remove(
														panel.ownerCt, true);
												for(var p=0;this.serNoIds.length;p++){
													var s = this.serNoIds[p];
													if(pId==s){
														this.serNoIds[p]='';
														break;
													}
													
												}
											}.createDelegate(this),
											failure : function(response) {

											}
										});
									}
								}.createDelegate(this));
							}.createDelegate(this)
						}];
						var p = new Ext.Panel({
							title : fileName,
							flag : true,
							autoScroll : true,
							pId : fileName,
							attachId : fileId,
							tools : loadTools,
							html : "<img id ="
									+ fileName
									+ " align='center' width=100% height=250 src='"
									+ downUrl + "'/>"
						});
						this.portal.add({
							columnWidth : .5,
							style : 'padding:10px 0 0px 10px',
							items : [p]
						});
						this.serNoIds.push(fileName);
						this.comArr.push(p);
						this.portal.doLayout();
//						new Ext.Resizable(fileName, {
//							wrap : true,
//							pinned : true,
//							minWidth : 50,
//							minHeight : 50,
//							preserveRatio : true,
//							dynamic : true,
//							handles : 'all',
//							draggable : true
//						});
					}
				}
				
			}.createDelegate(this)
		});

	}
});